#include "StdAfx.h"
#include "UIMenu.h"
#include "UIControl.h"
#include "UIGlobalStyle.h"
#include "UIWindowDC.h"

CUIMenu::CUIMenu( HWND hWnd, HWND hMenu ): m_hOwner( hWnd ), m_hWndMenu( hMenu )
{

}

CUIMenu::~CUIMenu( void )
{

}

LRESULT CUIMenu::OnPaint()
{
//	CUIWindowDC dc(GetHwnd());
    HDC hDC =::GetWindowDC( m_hOwner );
    RECT rcWnd;
    ::GetWindowRect( m_hOwner, &rcWnd );
    ::FillRect( hDC, &rcWnd, ( HBRUSH )::GetStockObject( BLACK_BRUSH ) );
    ::ReleaseDC( m_hOwner, hDC );
    return 0;
}

LRESULT CUIMenu::OnMeasureItem( WPARAM wParam, LPARAM lParam )
{
    MEASUREITEMSTRUCT* pMenuStruct = ( MEASUREITEMSTRUCT* )lParam;

    if ( ODT_MENU == pMenuStruct->CtlType )
    {
        MENUITEMINFO    itemInfo;

        memset( &itemInfo, 0, sizeof( MENUITEMINFO ) );
        itemInfo.cbSize = sizeof( MENUITEMINFO );

        itemInfo.fMask = MIIM_TYPE;
        ::GetMenuItemInfo( m_hMenu, pMenuStruct->itemID, FALSE, &itemInfo );

        if ( MFT_SEPARATOR == ( itemInfo.fType & MFT_SEPARATOR ) )
        {
            pMenuStruct->itemWidth = 150;
            pMenuStruct->itemHeight = 5;
        }
        else
        {
            pMenuStruct->itemWidth = 150;
            pMenuStruct->itemHeight =::GetSystemMetrics( SM_CYMENUSIZE );
        }
    }

    return TRUE;
}

LRESULT CUIMenu::OnDrawItem( WPARAM wParam, LPARAM lParam )
{
    DRAWITEMSTRUCT* pDrawStruct = ( DRAWITEMSTRUCT* )lParam;
    RECT rcItem = pDrawStruct->rcItem;
    HWND hItem = pDrawStruct->hwndItem;
    HDC hDC = pDrawStruct->hDC;

    if ( ODT_MENU == pDrawStruct->CtlType )
    {
        HBRUSH hBrush = ( HBRUSH )NULL_BRUSH;

        if ( pDrawStruct->itemState & ODS_SELECTED )
        {
            hBrush = ( HBRUSH )::CreateSolidBrush( CUIGlobalStyle::GetEdgeHighLightColor() );
        }
        else
        {
            hBrush = ( HBRUSH )::CreateSolidBrush( CUIGlobalStyle::GetEdgeShadowColor() );
        }

        HBRUSH hOldBrush = ( HBRUSH )::SelectObject( hDC, hBrush );
        ::FillRect( hDC, &rcItem, hBrush );
        ::SelectObject( hDC, hOldBrush );
        ::DeleteObject( hOldBrush );
        ::DeleteObject( hBrush );
        TCHAR pStr[50] = {0};
        ::GetMenuString( ( HMENU )hItem, pDrawStruct->itemID, pStr, 50, MF_BYCOMMAND );
        ::SetBkMode( hDC, TRANSPARENT );
        ::DrawText( hDC, pStr, _tcslen( pStr ), &rcItem, DT_VCENTER | DT_SINGLELINE );
    }

    return TRUE;
}

LRESULT CUIMenu::OnInitMenuPopup( WPARAM wParam, LPARAM lParam )
{
    m_hMenu = ( HMENU )wParam;
    BOOL bSysMenu = HIWORD( lParam );
    DWORD dwStyle;
    dwStyle = GetWindowLong( m_hWndMenu, GWL_EXSTYLE );
    dwStyle &= ~WS_EX_WINDOWEDGE;
    dwStyle = SetWindowLong( m_hWndMenu, GWL_EXSTYLE,
                             dwStyle );
    //		SetLayeredWindowAttributes(hMenuWnd, 0, 150, LWA_ALPHA);


    int iMenuCount =::GetMenuItemCount( m_hMenu );

    for ( int i = 0; i < iMenuCount; i++ )
    {
        MENUITEMINFO    itemInfo;

        memset( &itemInfo, 0, sizeof( MENUITEMINFO ) );
        itemInfo.cbSize = sizeof( MENUITEMINFO );

        itemInfo.fMask = MIIM_STATE | MIIM_TYPE;
        ::GetMenuItemInfo( m_hMenu, i, TRUE, &itemInfo );
        int itemID = ::GetMenuItemID( m_hMenu, i );

        if ( ( itemInfo.fType & MFT_SEPARATOR ) != MFT_SEPARATOR )
        {
            ::ModifyMenu( m_hMenu,
                          i,
                          itemInfo.fState | MF_BYPOSITION | MF_OWNERDRAW,
                          itemID,
                          ( LPCTSTR )i );
        }
        else
        {
            ::ModifyMenu( m_hMenu,
                          i,
                          MF_BYPOSITION | MF_OWNERDRAW | MF_SEPARATOR,
                          itemID,
                          ( LPCTSTR )i );
        }
    }

    return 0;
}

